package com.filldream.fastboot.core.manager;

import com.filldream.fastboot.base.mapper.CfArticleMapper;
import com.filldream.fastboot.common.entity.B;
import com.filldream.fastboot.common.entity.R;
import com.filldream.fastboot.common.entity.db.CfArticle;
import com.filldream.fastboot.common.okutil.RobotUtil;
import com.filldream.fastboot.common.okutil.cls.dto.ClsNewsSimpleItem;
import com.filldream.fastboot.common.okutil.stock.cf.EastCfUtil;
import com.filldream.fastboot.common.util.JsonUtil;
import com.filldream.fastboot.core.service.websocket.ClsNewsServer;
import com.filldream.fastboot.core.thread.ThreadPoolUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service
public class CfArticleManager {

    @Resource
    private CfArticleMapper cfArticleMapper;

    /**
     * 获取资讯
     * 及时获取，将没有的资讯记录在表
     * @param stockCode 股票代码
     * @return
     */
    public R<List<CfArticle>> refreshArticle(String stockCode) {
        List<CfArticle> addList = new ArrayList<>();
        List<CfArticle> articleList = EastCfUtil.getArticles(stockCode);
        if(ObjectUtils.isNotEmpty(articleList)){
            List<Long> ids = cfArticleMapper.getPostIds();
            for (CfArticle article : articleList) {
                //不存在则新增
                boolean hasFlag = ids.stream().anyMatch(t -> t.equals(article.getPostId()));
                if( !hasFlag ){
                    int insert = cfArticleMapper.insert(article);
                    if(insert > 0){
                        addList.add(article);
                    }
                }
            }
        }
        //推送消息到ws
        ThreadPoolUtil.R.getExecutor().execute(() -> {
            List<ClsNewsSimpleItem> newsList = new LinkedList<>();
            addList.forEach(t -> newsList.add(t.toClsNewsSimpleItem()));
            new ClsNewsServer().sendNewsList(newsList);
        });
        return R.autoSuccess(addList);
    }

    /**
     * 自动发送消息给机器人
     * @return
     */
    public B autoSendOwner(List<String> senOwnerList){
        List<ClsNewsSimpleItem> zrWaitList = new ArrayList<>();
        List<ClsNewsSimpleItem> sunWaitList = new ArrayList<>();
        List<CfArticle> addList = cfArticleMapper.getNotSendOwnerList(senOwnerList);
        for (CfArticle article : addList) {
            List<String> sendOwnerList = article.getSendOwnerList();
            if( !sendOwnerList.contains("zr") ){
                zrWaitList.add(article.toClsNewsSimpleItem());
                sendOwnerList.add("zr");
            }
            if( !sendOwnerList.contains("sun") ){
                sunWaitList.add(article.toClsNewsSimpleItem());
                sendOwnerList.add("sun");
            }
            article.setSendOwner(JsonUtil.toJson(sendOwnerList));
            cfArticleMapper.updateById(article);
        }
        //发消息
        RobotUtil.autoDD( sunWaitList );
        RobotUtil.autoFs( zrWaitList );
        return B.success();
    }

    /**
     * 获取重点关注的股票配置代码
     * @return
     */
    public List<String> getRobotStockCode() {
        return cfArticleMapper.getRobotStockCode();
    }


}
